RenderSystem = System.new()
RenderSystem.scissors = {}
RenderSystem.renderSkip = {}
function RenderSystem:draw()
    if self.pasued then
        return
    end
    self:_draw()
end

function RenderSystem:_draw()
    self.batches = {}
    self.curBatch = nil
    self:collectRenderInfo(Node.Root)
    self:drawBatches()
    -- local labels = {
    --     "canvases",
    --     "texturememory",
    --     "drawcalls",
    --     "drawcallsbatched",
    --     "fonts",
    --     "canvasswitches",
    --     "shaderswitches",
    --     "images",
    --     -- "fps"
    -- }

    -- love.graphics.setColor(0, 0, 0, 1)
    -- local sts = love.graphics.getStats()
    -- for i, key in ipairs(labels) do
    --     if sts[key] then
    --         love.graphics.print(key..": "..sts[key], 50, i * 20)

    --     end
    -- end
    -- love.graphics.print("fps: "..love.timer.getFPS(), 50, 180)
    -- love.graphics.setColor(1, 1, 1, 1)

    if RenderSystem.drawGrid then
        local w, h = love.graphics.getDimensions()
        local size = 50
        local c = math.floor(w / size)
        local r = math.floor(h / size)
        love.graphics.setColor(0.5, 0.5, 0.5, 0.5)
        for i = 1, c do
            local x = i * size
            love.graphics.line(x, 0, x, h)
        end
        for i = 1, r do
            local y = i * size
            love.graphics.line(0, y, w, y)
        end
        love.graphics.setColor(1, 1, 1, 1) 
    end
end

function RenderSystem:newBatch(renderInfo)
    self.curBatch = RenderBatch.new(renderInfo)
    table.insert(self.batches, self.curBatch)
end
function RenderSystem:collectRenderInfo(node)
    if self.renderSkip[node] then
        return
    end
    if not node.active then
        return
    end
    if node:hasCompoent(Scissor) then
        local renderInfo = node:getScissorInfo()
        self:newBatch(renderInfo)
    end
    if node.renderable then
        local renderInfo = node:getRenderInfo()
        if not self.curBatch then
            self:newBatch(renderInfo)
        elseif not self.curBatch:tryBatch(renderInfo) then
            self:newBatch(renderInfo)
        end
    end
    for _, v in ipairs(node.children) do
        self:collectRenderInfo(v)
    end
    if node:hasCompoent(Scissor) then
        self:newBatch({renderType = RenderType.ScissorOff})
    end
end
function RenderSystem:drawBatches()
    for i, b in ipairs(self.batches) do
        if b.renderType == RenderType.Scissor then
            self:pushScissor(unpack(b.args))
        elseif b.renderType == RenderType.ScissorOff then
            self:popScissor()
        else
            b:draw()
        end
    end
end

function RenderSystem:update(dt)
end

function RenderSystem:pushScissor(x, y, width, height)
    table.insert(self.scissors, {x, y, width, height})
    love.graphics.setScissor()
    for i = 1, #self.scissors do
        love.graphics.intersectScissor(unpack(self.scissors[i]))
    end
end

function RenderSystem:popScissor()
    table.remove(self.scissors, #self.scissors)
    love.graphics.setScissor()
    if #self.scissors > 0 then
        for i = 1, #self.scissors do
            love.graphics.intersectScissor(unpack(self.scissors[i]))
        end
    end
end

RegisterSystem(RenderSystem)